﻿Public Class DRUGUSE
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub filterDrugug()
        Dim s2 As FILTERCLASS
        Dim sql2 As String = "SELECT CAST(ugid AS CHAR(8)) AS UGID, ugname, ugname_en, f_food FROM masdrgug"
        s2 = New FILTERCLASS(TxtUGID, sql2, "รหัส,การใช้(ไทย),การใช้(อังกฤษ),กิน", "50,110,110,50", "1,1,1,1", "1,1,1,0")
        's.SetShowInDown = False
        s2.SetShowBorder = True
    End Sub

    Private Sub DRUGUSE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridMASDRGUSE()

        DesignForm(Me)
        DesignGrid(MASDRGUSEGrid)
        filterDrugug()

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridMASDRGUSE()
        Dim objSelectDataMASDRGUSE As New masdrguse
        Dim MASDRGUSE As DataTable
        MASDRGUSE = connect.GetTable(objSelectDataMASDRGUSE.selectMasdrguse_)
        MASDRGUSEGrid.PrimaryGrid.DataSource = MASDRGUSE
    End Sub

    Private Sub MASDRGUSEGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASDRGUSEGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value() IsNot DBNull.Value Then
            useidTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            useidTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเภทการใช้ยา").Value() IsNot DBNull.Value Then
            TxtUGID.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเภทการใช้ยา").Value()
        Else
            TxtUGID.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วิธีการใช้ยา").Value() IsNot DBNull.Value Then
            DUSENAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วิธีการใช้ยา").Value()
        Else
            DUSENAMETxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วิธีการใช้ยา (Eng)").Value() IsNot DBNull.Value Then
            TxtDUSENAME_EN.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วิธีการใช้ยา (Eng)").Value()
        Else
            TxtDUSENAME_EN.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            masdrguseStatusChk.Checked = True
        Else
            masdrguseStatusChk.Checked = False
        End If

        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE masdrguse SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `useid` = '" & useidTxt.Text & "' ;"

                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
        addMasdrguse.Enabled = False
    End Sub

    Private Sub addMasdrguse_Click(sender As Object, e As EventArgs) Handles addMasdrguse.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If TxtUGID.Text.ToString.Length > 0 And DUSENAMETxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New masdrguse
                    objInsertData.UGID_ = TxtUGID.Tag
                    objInsertData.DUSENAME_ = DUSENAMETxt.Text.Trim
                    objInsertData.DUSENAME_EN_ = TxtDUSENAME_EN.Text.Trim
                    objInsertData.STATUS_ = masdrguseStatusChk.Checked
                    objInsertData.insertMasdrguseData()

                    SelectDatagridMASDRGUSE()

                    useidTxt.Text = ""
                    TxtUGID.Text = ""
                    DUSENAMETxt.Text = ""
                    masdrguseStatusChk.Checked = False
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMasdrguse_Click(sender As Object, e As EventArgs) Handles editMasdrguse.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If TxtUGID.Text.ToString.Length > 0 And DUSENAMETxt.Text.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลการใช้ยาดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        Dim objEditData As New masdrguse
                        objEditData.USEID_ = useidTxt.Text
                        objEditData.UGID_ = TxtUGID.Text
                        objEditData.DUSENAME_ = DUSENAMETxt.Text.Trim
                        objEditData.DUSENAME_EN_ = TxtDUSENAME_EN.Text.Trim
                        objEditData.STATUS_ = masdrguseStatusChk.Checked
                        objEditData.editMasdrguseData()

                        SelectDatagridMASDRGUSE()

                        useidTxt.Text = ""
                        TxtUGID.Text = ""
                        DUSENAMETxt.Text = ""
                        TxtDUSENAME_EN.Text = ""
                        masdrguseStatusChk.Checked = False
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
                addMasdrguse.Enabled = True
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("masdrguse")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("masdrguse")
        nextform.ShowDialog()
    End Sub
End Class